package com.newtouch.bxzs.common.base.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Feign配置类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021年3月4日</td>
 * <td>Amour</td>
 * <td>Create</td>
 * </tr>
 * </table>
 * 
 * @author Amour
 * 
 * @version 1.0.0
 * @since 1.0.0
 */
@Configuration
public class FeignConfig implements RequestInterceptor {
	@Override
	public void apply(RequestTemplate requestTemplate) {
		String header = "";
		try {
			ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
					.getRequestAttributes();
			HttpServletRequest request = attributes.getRequest();
			header = request.getHeader("Authorization");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (StringUtils.isEmpty(header)) {
				requestTemplate.header("Authorization", header);
			}
		}
	}
}
